package Controller;

import POJO.InputPOJO.UserPage;
import POJO.Model.PageResponseModel;
import POJO.Model.ResponseModel;
import POJO.User;
import POJO.Vo.UserVo;
import Service.UserService;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller("UserController")
public class UserController {
    @Resource
    UserService userService;
    @Resource
    ResponseModel responseModel;
    @Resource
    PageResponseModel pageResponseModel;

    /*新增登录员*/
    @RequestMapping(value = "/logon/new" )
    public @ResponseBody ResponseModel addUser(@RequestBody User user)
    {
        userService.insertUser(user);
        return responseModel;
    }


    /*登录员查询*/
    @RequestMapping(value = "/logon/list")
    public @ResponseBody PageResponseModel select(@RequestBody UserPage userPage)
    {
        List<UserVo> list=userService.userList(userPage);
        pageResponseModel.setData(list);
        return pageResponseModel;
    }

    /*登录员修改*/
    @RequestMapping(value = "/logon/update")
    public  @ResponseBody ResponseModel updateUser(@RequestBody User user)
    {
        userService.updateUser(user);
        return  responseModel;
    }

    /*登录员删除*/
    @RequestMapping(value = "/logon/remove")
    public @ResponseBody ResponseModel deleteUser(@RequestBody int userId)
    {
        userService.deleteUser(userId);
        return responseModel;
    }

    /*用户登录*/
    @RequestMapping(value = "/logon")
    public @ResponseBody ResponseModel logon (@RequestBody User user)
    {
        UserVo userVo=userService.logon(user);
        if (userVo!=null) {
            responseModel.setCode(200);
            responseModel.setMsg("登陆成功");
            responseModel.setData(userVo);
        }
        else {
            responseModel.setCode(218);
            responseModel.setMsg("密码错误");
        }
        return responseModel;
    }
}
